package com.itheima.edu.info.manager.service;

import com.itheima.edu.info.manager.dao.TeacherDao;

import com.itheima.edu.info.manager.domain.Teacher;

public class TeacherService {
    private  TeacherDao teacherDao = new TeacherDao();


    public  Teacher[] findAllTeacher() {
        //1.调用库管对象的findAllStudent获取学生对象数组

        Teacher[] allTeacher = teacherDao.findAllTeacher();
        //2.判断数组中是否有学生信息(有的话,返回学生信息,没有的话返回null)
        boolean flag = false;
        for (int i = 0; i < allTeacher.length; i++) {
            Teacher tec = allTeacher[i];
            if (tec != null){
                flag = true;
                break;
            }
        }
        if(flag){
            return allTeacher;

        }else {
            return null;
        }
        //思路:数组中只要存在一个不是null的元素,就代表数组中有学生信息
    }

    /**
     * 添加老师方法
     * @param tec
     * @return
     */
    public boolean addTeacher(Teacher tec) {

        return teacherDao.addTeacher(tec);
    }

    /**
     * 判断老师是否存在方法
     * @param id
     * @return
     */
    public boolean isExists(String id) {
        Teacher[] tecs = teacherDao.findAllTeacher();
        //假设ID在数组中不存在
        boolean exists = false;
        //遍历数组,获取每一个学生对象,准备进行判断
        for (int i = 0; i < tecs.length; i++) {
            Teacher tsc = tecs[i];
            if (tsc != null && tsc.getId().equals(id)){
                exists = true;
                break;
            }
        }
        return exists;
    }

    public void deleteTeacherById(String delId) {
        teacherDao.deleteTeacherById(delId);
    }

    public void updateTeacher(String updateId, Teacher newtec) {
        teacherDao.updateTeacher(updateId,newtec);
    }
}
